<template>
  <div class="footerReply-warp">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
      <el-form-item prop="context">
        <el-input
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 4}"
          placeholder="请输入内容"
          v-model="ruleForm.context"
        ></el-input>
      </el-form-item>
      <div class="bottom-button">
        <el-select v-model="template" size="mini" @change="_selectTemplate" placeholder="选择模板">
          <el-option label="选择模板" value></el-option>
          <el-option
            v-for="item in data"
            :key="item.mailTemplateId"
            :label="item.mailTemplateName"
            :value="item.context"
          ></el-option>
        </el-select>
        <el-button type="primary" size="mini" @click="_replyEmail('ruleForm')" :loading="loading">发送</el-button>
      </div>
    </el-form>
  </div>
</template>
<script>
const ERR_OK = 200;
export default {
  props: ['contentItem'],
  data() {
    return {
      ruleForm: {
        subject: "WARRANTY",
        context: ""
      },
      loading:false,
      template: "",
      data: [],
      rules: {
        context: [{ required: true, message: "请输入内容", trigger: "blur" }]
      }
    };
  },
  activated() {
    this.findAll();
  },
  mounted() {
    this.findAll();
  },
  methods: {
    findAll() {
      this.$ajax( this.GLOBAL.warrantyService + "/mailTemplate/findAll?businessName=" + this.$route.name.substring(0,2)).then(res => {
        if(res.data.code === ERR_OK){
           this.data = res.data.data;
        }
      });
    },
    // 选择模板
    _selectTemplate(val) {
      this.ruleForm.context = val;
    },
    // 回复邮件
    _replyEmail(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.loading = true
          let _url = "";
          this.ruleForm.emailAddress = this.contentItem.emailAddress;
           this.ruleForm.businessName = this.$route.name.substring(0,2);
          if (!this.contentItem.isSend) {
            _url = "/sendMail/reply";
          } else {
            _url = "/sendMail/send";
          }
          this.$ajax({
            method: "post",
            url:  this.GLOBAL.warrantyService + _url,
            data: this.ruleForm
          }).then(res => {
            if (res.data.code === ERR_OK) {
              this.ruleForm.context = "";
              this.$emit("reply");
              this.$message.success("回复成功");
            } else {
              this.$message.error("发送失败");
            }
            this.loading = false
          });
        } else {
          this.loading = false
          return false;
        }
      });
    }
  }
};
</script>
<style lang="stylus" scoped>
.footerReply-warp {
  border-top: 1px solid #dcdfe6;
  background: #eef2f6;
  padding: 10px;

  .el-select {
    width: 120px;
  }

  .bottom-button {
    padding-top: 10px;
    text-align: right;
  }
}
</style>


